<?php
include("UserIncludes/DbConfig.php");

// All Campaign which is valid accordint to Start and End Date

$query_all_campaign = "select * from adcampaign where start_date <= CURDATE() AND end_date >= CURDATE()";

echo $query_all_campaign;

$res_all_campaign = mysql_query($query_all_campaign);

// retrieving all adgroup_ad from the above query
echo "<br><br>";
while($row_all_campaign = mysql_fetch_array($res_all_campaign))
	{

		$query_all_ad = "Select * from adgroup_ad where campaign_id = ".$row_all_campaign['campaign_id']." order by adgroup_ad_id asc ";
		$res_all_ad  = mysql_query($query_all_ad);
		
		if(mysql_num_rows($res_all_ad)>0){
		while($row_all_ad = mysql_fetch_array($res_all_ad))
		{
			// For direct network only
			$adgroup_id = "select * from adgroup where adgroup_id = '".$row_all_ad['adgroup_id']."' ";
			$res_adgroup_id = mysql_query($adgroup_id);
			$row_adgroup_id = mysql_fetch_array($res_adgroup_id);

 			if($row_adgroup_id['network_type'] == 'direct') { //For Direct Network

			$query_network_id = "SELECT * from network_master where networkId =  ".$row_adgroup_id['adgroup_network']." ";

			$res_network_id = mysql_query($query_network_id);
			$row_network_id = mysql_fetch_array($res_network_id);

			echo "Direct Ads--->"."      ".$row_all_ad['heading']."   ".$row_all_ad['ad_description']."   ".$row_all_ad['dest_url']."   ".$row_all_ad['personal_reference']."   <br>";
			echo "`            Network Id from network_master table--->".$row_network_id['networkId']."<br><br>";

			
							   		}

			else						{ //For Custom Network

			$query_network_id = "SELECT * from create_network where network_id =  ".$row_adgroup_id['adgroup_network']." ";
			//echo "<br>Query--->".$query_network_id."<br>";
			$res_network_id = mysql_query($query_network_id);
			$row_network_id = mysql_fetch_array($res_network_id);
			
			if(mysql_num_rows($res_network_id)>0)	{
			echo "<br><br>Custom Ads-->"."      ".$row_all_ad['heading']."   ".$row_all_ad['ad_description']."   ".$row_all_ad['dest_url']."   ".$row_all_ad['personal_reference']."   <br>";
			echo "Category---->".$row_network_id['category']."<br><br>";
			$split_category = array();
			$split_category = explode(",",$row_network_id['category']);
			
			for($i=0;$i<sizeof($split_category);$i++)
			{
          $query_category_id = "SELECT adcategoryId from adcategories where categoryName = '".$split_category[$i]."' ";
          $res_category_id = mysql_query($query_category_id);
          $row_category_id = mysql_fetch_array($res_category_id);
          //echo "Category Id----->".$row_category_id['adcategoryId'];
          
          $query_site = "SELECT * from adsite_master where adcategoryId = ".$row_category_id['adcategoryId']." ";
          $res_site = mysql_query($query_site);
          echo "Site Name-->";
          while($row_site = mysql_fetch_array($res_site))
          {
            echo "<br>".$row_site['adSiteUrl'];
          }
          
          
      
      }
			

								}
			

							   		}
 	 	 		
		}
						 }
echo "<br><br><br><br><br><br><br><br><br><br>";
	}


?>